* PANEL B OF TABLES 1 AND 2: DiD ESTIMATIONS COMPARING OW TO ALL OTHER CANTONS * 

/*
Tables of the paper: 
		- Table 1, Panel B (analysis at canton level)
		- Table 2, Panel B (analysis at canton level)
*/


// NOTE: data needed for this do-file cannot be shared due to privacy reasons

/* following packages need to be installed:
ssc install winsor2
ssc install reghdfe
ssc install boottest
ssc install estout
*/


version 16.1

clear all
cap log close 
cap clear matrix
set more off
cap set scheme mygraphs



*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

*   LOAD & PREPARE THE DATA for analysis at canton level, without controls
*	- columns 1-5 

run "$mypathRR/Resources/prep_canton_level_data.do"

global yrs20012016 "year > 2000 & year < 2017"
global yrs19952016 "year > 1994 & year < 2017"
global yrsall "year =!."

* only years for which data on OW is available
drop if year < 1995
drop if (year == 1996 | year == 1998 | year == 1999 | year == 2000) 

// only look at those outcomes where parallel trends look ok according to ES-estimates
global outcomes "share300K share200K share300Kc share300Kc2 reink reink_w reink_ctrl reink_tr reink_tr_w"


foreach outcome in $outcomes {
// m0: baseline (cant & time FE, weights, no clustering)
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(cant year)
}
else {
		reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(cant year)
}
eststo m0_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
			qui reghdfe `outcome' Interaction treated period ib2005.period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(cant)
		}
		else {
			qui reghdfe `outcome' Interaction treated period ib2005.period [aweight = weight_`outcome'] if  $yrs19952016 , absorb(cant)
		}
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m0_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m0_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m0_`outcome' = boot_ci_hi

* same model with 2-way clustering		
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(cant year) vce(cluster cant year)
}
else {
		reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(cant year) vce(cluster cant year)
}

tempname SE2W clCant clYear
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCant' 	= 26
scalar define `clYear' 	= 18

eststo m0_2w_`outcome'		

// add stats to be passed on to estout
	estimates restore m0_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m0_`outcome'} 
	estadd scalar upper = ${hi_m0_`outcome'}
	estadd scalar clusters = 26
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m0_`outcome'
	
	
	estimates restore m0_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m0_2w_`outcome'
}	

		
foreach outcome in $outcomes {
// m1: baseline (cant & time FE, weights, no clustering, correcting for pre-trend)
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(cant year)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(cant year)
}
eststo m1_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
			qui reghdfe resid`outcome' Interaction treated period ib2005.period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(cant)
		}
		else {
			qui reghdfe resid`outcome' Interaction treated period ib2005.period [aweight = weight_`outcome'] if  $yrs19952016 , absorb(cant)
		}
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m1_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m1_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m1_`outcome' = boot_ci_hi

* same model with 2-way clustering
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(cant year) vce(cluster cant year)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(cant year) vce(cluster cant year)
}

tempname SE2W clCant clYear
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCant' 	= 26
scalar define `clYear' 	= 18

eststo m1_2w_`outcome'	

// add stats to be passed on to estout
	estimates restore m1_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m1_`outcome'} 
	estadd scalar upper = ${hi_m1_`outcome'}
	estadd scalar clusters = 26
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m1_`outcome'
	
	estimates restore m1_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m1_2w_`outcome'
	
}	

	
foreach outcome in $outcomes {
// m4: CLUSTER (cant & time FE, weights, clustered at canton level)
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , vce(cluster cant) absorb(cant year)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , vce(cluster cant) absorb(cant year)
}
eststo m4_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
			qui reghdfe resid`outcome' Interaction treated period ib2005.period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , vce(cluster cant) absorb(cant)
		}
		else {
			qui reghdfe resid`outcome' Interaction treated period ib2005.period [aweight = weight_`outcome'] if $yrs19952016 , vce(cluster cant) absorb(cant)
		}
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m4_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m4_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m4_`outcome' = boot_ci_hi
		
* same model with 2-way clustering
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , vce(cluster cant year) absorb(cant year)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , vce(cluster cant year) absorb(cant year)
}

tempname SE2W clCant clYear
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCant' 	= 26
scalar define `clYear' 	= 18

eststo m4_2w_`outcome'	

// add stats to be passed on to estout
	estimates restore m4_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m4_`outcome'} 
	estadd scalar upper = ${hi_m4_`outcome'}
	estadd scalar clusters = 26
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m4_`outcome'
	
	estimates restore m4_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m4_2w_`outcome'
}



*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

*   LOAD & PREPARE THE DATA for analysis at canton level, with controls
*	- column 6

run "$mypathRR/Resources/prep_canton_level_data_controls.do"

global yrs20012016 "year > 2000 & year < 2017"
global yrs19952016 "year > 1994 & year < 2017"
global yrsall "year =!."

* only years for which data on OW is available
drop if year < 1995
drop if (year == 1996 | year == 1998 | year == 1999 | year == 2000) 

// only look at those outcomes where parallel trends look ok according to ES-estimates
global outcomes "reink"

global controls " nowork retiree self married singlep marriedch "
	
foreach outcome in $outcomes {
// m4: CLUSTER (mun & time FE, weights, clustered at canton level)
reghdfe resid`outcome' Interaction treated period $controls [aweight = weight_`outcome'] if $yrs19952016, vce(cluster cant) absorb(cant year)
eststo m4c_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		qui reghdfe resid`outcome' Interaction treated period $controls ib2005.period [aweight = weight_`outcome'] if $yrs19952016, vce(cluster cant) absorb(cant)
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m4c_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m4c_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m4c_`outcome' = boot_ci_hi
		
* same model with 2-way clustering
reghdfe resid`outcome' Interaction treated period $controls [aweight = weight_`outcome'] if $yrs19952016, vce(cluster cant year) absorb(cant year)

tempname SE2W clCant clYear
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCant' 	= 26
scalar define `clYear' 	= 18

eststo m4c_2w_`outcome'	

// add stats to be passed on to estout
	estimates restore m4c_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m4c_`outcome'} 
	estadd scalar upper = ${hi_m4c_`outcome'}
	estadd scalar clusters = 26
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m4c_`outcome'	
	
	estimates restore m4c_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCant'
	estadd scalar clust2 = `clYear'
	estimates store m4c_2w_`outcome'	
}	






*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

// OUTPUT TABLES TO LATEX
cd "$mypathRR/Results/"	

* TABLE 1, panel B with 2-way clustering
estout m0_2w_share300K m1_2w_share300K  using "Tab1_PanelB)-DID-share300K-2wclust.tex" 			///
, replace style(tex) cells( "b(star fmt(3))") 					///
keep(Interaction) varlabels(Interaction "DiD") 									///
stats(se2w N r2 clust1 clust2 , 											///
fmt(%9.2f   %9.0g  %9.2f %9.0f %9.0f %9.3f ) layout("[@]" @ @ @ @ )			///
labels("2-way cluster SE"  "\addlinespace[1.5ex] N" "R$^2$" " \addlinespace[1.5ex]  Canton clusters" "Year clusters" )) ///
mlabels(none)  collabels(none)  	


* TABLE 2, panel B with winsorized values and 2-way clustering
estout m0_2w_reink_w m1_2w_reink_w  m4_2w_reink_ctrl m4_2w_reink_tr_w m4c_2w_reink using "Tab2_PanelB)-DID-reink_winsor-2wclust.tex" 			///
, replace style(tex) cells( "b(star fmt(2))") 					///
keep(Interaction) varlabels(Interaction "DiD") 									///
stats(se2w N r2 clust1 clust2 , 											///
fmt(%9.2f   %9.0g  %9.2f %9.0f %9.0f %9.3f ) layout("[@]" @ @ @ @ )			///
labels("2-way cluster SE"  "\addlinespace[1.5ex] N" "R$^2$" " \addlinespace[1.5ex] Canton clusters" "Year clusters" )) ///
mlabels(none)  collabels(none)  					


						* * * * *  E N D  * * * * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
